package com.hj.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hj.dao.RoleMapper;
import com.hj.dao.RoleMenuMapper;
import com.hj.form.RoleMenuForm;
import com.hj.pojo.Role;
import com.hj.pojo.RoleExample;
import com.hj.pojo.RoleExample.Criteria;
import com.hj.pojo.RoleMenu;
import com.hj.service.IRoleService;

@Service
public class RoleServiceImpl implements IRoleService {

	@Autowired
	RoleMapper roleMapper;
	@Autowired
	RoleMenuMapper roleMenuMapper;
	
	@Override
	public PageInfo<Role> list(Integer page, Integer size, Role role) {
		// TODO Auto-generated method stub
		PageHelper.startPage(page, size);
		RoleExample ex = new RoleExample();
		Criteria criteria = ex.createCriteria();
		List<Role> list = roleMapper.selectByExample(ex);
		PageInfo<Role> pageInfo = new PageInfo<>(list);
		return pageInfo;
	}

	@Override
	public void add(Role role) {
		// TODO Auto-generated method stub
		roleMapper.insertSelective(role);
	}

	@Override
	public void delete(Integer id) {
		// TODO Auto-generated method stub
		roleMapper.deleteByPrimaryKey(id);
	}

	@Override
	public void update(Role role) {
		// TODO Auto-generated method stub
		roleMapper.updateByPrimaryKeySelective(role);
	}

	@Override
	public Role findOne(Integer id) {
		// TODO Auto-generated method stub
		return roleMapper.selectByPrimaryKey(id);
	}

	@Override
	public void addMenu(RoleMenuForm roleForm) {
		// TODO Auto-generated method stub
		// 需要操作角色权限表
		for (Integer mid : roleForm.getMids()) {
			RoleMenu rm = new RoleMenu();
			rm.setRid(roleForm.getRid());
			rm.setMid(mid);
			roleMenuMapper.insert(rm );
		}
		
	}

}
